import {
    SetExamcaseView,
    PostExamcaseShare,
    SetTaskScore,
    CheckWorkAuthority,
} from "~/api";

export const useContent = () => {
    const userStore = useUserStore();
    const pageStore = usePageStore();
    const dialogStore = useDialogStore();
    const url = useRequestURL();
    const payServiceRef = ref(null);
    const payRef = ref(null);

    const onJumpPath = (path, open, login, callback) => {
        if (!login || userStore.isLogin) {
            if (path) {
                typeof callback == "function" && callback();
                if (path.startsWith("http")) {
                    window.open(path);
                } else {
                    navigateTo(path, {
                        open,
                    });
                }
            }
        } else {
            LoginModal(true, { url: path }, {}, open);
        }
    };

    const onJump = async (item) => {
        // 判断是关联作品
        if (item.work_id) {
            if (userStore.isLogin) {
                // 判断是否有权限
                const res = await CheckWorkAuthority({
                    work_id: item.work_id,
                });

                if (res.data) {
                    onJumpPath(
                        item.templatetype_code == "template"
                            ? item.url
                            : item.url.replace(
                                "mshare.vrnewg.com",
                                "kj-mshare.vrnewg.com"
                            ),
                        !pageStore.isMobile,
                        true,
                        () => {
                            SetExamcaseView({ id: item.id }).then((res) => {
                                item.page_view = res.data;
                            });
                        }
                    );
                } else {
                    Message().warning("请先购买!");
                }
            } else {
                LoginModal();
            }
        } else if (item.type == 1) {
            onJumpPath("/caseDetail/" + item.id + ".html", false, false);

            if (dialogStore.isMidListDialogVisible) {
                dialogStore.isMidListDialogVisible = false;
            }
        } else {
            if (userStore.isLogin) {
                SetExamcaseView({ id: item.id }).then((res) => {
                    item.page_view = res.data;
                });

                SetTaskScore({
                    code: 1012,
                    _id: item.id,
                });
                // 判断是mp4
                if (/\.mp4/i.test(item.url)) {
                    navigateTo("/caseView/" + item.id + ".html", {
                        open: !pageStore.isMobile,
                    });
                } else {
                    window.open(item.url, pageStore.isMobile ? "_self" : "_blank");
                }
            } else {
                LoginModal();
            }
        }
    };

    const onBuy = async (item) => {
        if (userStore.isLogin) {
            // 判断是否需要购买
            const res = await CheckWorkAuthority({
                work_id: item.work_id,
            });

            if (!res.data) {
                payServiceRef.value.show({
                    name: item.name,
                    file: item.file,
                    price: item.price,
                });
            } else {
                Message().warning("当前作品已购买!");
            }
        } else {
            LoginModal();
        }
    };

    const onPaySuccess = (item) => {
        item.is_buy = true;
    };

    const onComment = (item) => {
        if (userStore.isLogin) {
            dialogStore.isCaseCommentListDialogVisible = true;
            dialogStore.commentCaseId = item.id;
        } else {
            LoginModal();
        }
    };

    const onPayConfirm = (item) => {
        payRef.value.show({ id: item.work_id });
    };

    const onShare = async (item) => {
        if (item.type == 1) {
            dialogStore.isShareTemplateDialogVisible = true;
            dialogStore.shareUrl =
                url.origin + "/caseDetail/" + item.id + ".html";
            dialogStore.shareTitle = "案例链接";
            dialogStore.shareName = item.name;
        } else if (item.type == 2) {
            dialogStore.isShareTemplateDialogVisible = true;
            dialogStore.shareTitle = "案例链接";
            dialogStore.shareName = item.name;

            if (/\.mp4/i.test(item.url)) {
                dialogStore.shareUrl =
                    url.origin + "/caseView/" + item.id + ".html";
            } else {
                dialogStore.shareUrl = item.url;
            }
        }

        SetTaskScore({
            code: 1013,
            _id: item.id,
        });

        const res = await PostExamcaseShare({
            cid: item.id,
        });

        if (res.code == 200) {
            item.share = res.data;
        }
    };

    return {
        payRef,
        payServiceRef,
        onBuy,
        onJump,
        onShare,
        onComment,
        onPayConfirm,
        onPaySuccess,
    };
};
